package com.ccjk.beusoft.thirdlib

import java.io.*

/**
 * Created by CCJK on 2017/12/21.
 */

object Util {
    fun copyFile(src: File, dest: File): Boolean {
        return try {
            copyFile(FileInputStream(src), dest)
        } catch (e: Exception) {
            false
        }
    }

    fun copyFile(src: InputStream, dest: File): Boolean {
        var inputStream: BufferedInputStream? = null
        var outputStream: BufferedOutputStream? = null
        try {
            if (!dest.exists()) {
                dest.createNewFile()
            }
            inputStream = BufferedInputStream(src)
            outputStream = BufferedOutputStream(FileOutputStream(dest))
            val buffer = ByteArray(1024 * 8)
            var len: Int
            do {
                len = inputStream.read(buffer, 0, buffer.size)
                if (len != -1) {
                    outputStream.write(buffer, 0, len)
                }
            } while (len != -1)
            outputStream.flush()
            return true
        } catch (e: Exception) {
            return false
        } finally {
            close(src)
            close(outputStream)
        }
    }

    fun close(closeable: Closeable?) {
        try {
            closeable?.close()
        } catch (e: IOException) {
        }
    }
}
